package com.xlh.controller;

import com.google.common.collect.Lists;
import com.xlh.common.Result;
import com.xlh.dto.course.ChapterBasicInfoDTO;
import com.xlh.dto.course.CourseDTO;
import com.xlh.enums.course.ChapterEnum;
import com.xlh.enums.course.CourseSharedEnum;
import com.xlh.service.course.ChapterService;
import com.xlh.service.course.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 962213579 2019/6/25
 */
@RestController
@Api(description = "课程章节下拉列表", value = "CourseChapterController")
@RequestMapping("/dropdown-list")
public class CourseChapterController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    @GetMapping("/course")
    @ApiOperation("课程下拉列表")
    public Result<List<CourseDTO>> courseDropdownList() {
        return Result.success(courseService.listCourse(CourseSharedEnum.PRESET));
    }

    @GetMapping("/course/{courseId}/chapter")
    @ApiOperation("章节下拉列表")
    public Result<List<ChapterBasicInfoDTO>> chapterDropdownList(@PathVariable("courseId") Long courseId) {
        String format = "第%d章 %s";
        List<ChapterBasicInfoDTO> chapterList = chapterService.listChapter(
                Lists.newArrayList(courseId), null, null, ChapterEnum.CHAPTER);
        if (CollectionUtils.isNotEmpty(chapterList)) {
            chapterList.forEach(chapter -> chapter.setName(String.format(format, chapter.getOrderNumber(), chapter.getName())));
        }
        return Result.success(chapterList);
    }

}
